"""
This is the second code to read huge sml file OMG... please.... 
"""
"""
Link: http://stackoverflow.com/questions/1513592/python-is-there-an-xml-parser-implemented-as-a-generator/1513640#1513640
"""

import os
import MySQLdb
import xml.etree.cElementTree as ElementTree
table_type = "posthistory"
table  = table_type+"_stackoverflow_aug13"

#"""
folder_path = "D:\Research\ISEC-2014\data\content\stackoverflow"
database = "isec2014"
user = "root"
password="123"
port=3306
dbfile  = "D:\Research\ISEC-2014\data\content\stackoverflow\meta.serverfault.com\PostHistory.xml"
"""

folder_path = "E:\Sangeeta\dataset\\2013_05_stack_overflow\Content\stackoverflow"
database = "sangeeta"
user = "sangeetal"
password="sangeetal"
port=3307
dbfile  = "E:\Sangeeta\dataset\\2013_05_stack_overflow\Content\stackoverflow.com\PostHistory.xml"
#"""

db1= MySQLdb.connect(host="localhost", user=user, passwd=password,db=database, port=port)
insert_cursor = db1.cursor() 


def fill_stackoverflow_table(dbfile):
    """Return a list of info dictionaries for all songs
    in a Rhythmbox library database file, with dictionary
    keys as given in @keys.
    """
    rhythmbox_dbfile = os.path.expanduser(dbfile)

    row=0
    try:
        for event, entry in ElementTree.iterparse(rhythmbox_dbfile):
        #print event, entry.tag, entry.attrib
        #text= (str)( entry.attrib['Id'])
        #print "text",text
        
        #break
        #if event=='end':
        #    print "end",event
        #    db1.commit()
            
            
            count=0
            Comment = ""
            Text = ""
            try:
                Comment=(str)(entry.attrib['Comment'])
                Commebt = Comment.replace("\n"," ")
                Comment = Comment.replace("'"," ")
                Comment = Comment.replace("\""," ")
                Comment = Comment.replace(",", " ")
                Comment = Comment.replace("(", " ")
                Comment = Comment.replace(")", " ")
                Comment = Comment.replace("\\", " ")
            except Exception ,e:
                #print "error"            
                count = count+1
                #print e, "count1=", count
        
        
            try:
                Text=(str)(entry.attrib['Text']).replace("\r"," ")
                Text = Text.replace("\n"," ")
                Text = Text.replace("'"," ")
                Text = Text.replace("\""," ")
                Text = Text.replace(",", " ")
                Text = Text.replace("("," ")
                Text = Text.replace(")", " ")
                Text = Text.replace("\\", " ")
            except Exception, e:
                #print "error"
                count = count+1
            try:
                UserId_or_name=(str)(entry.attrib['UserId'])
            except Exception, e:
                count  =count+1 
                try:
                    UserId_or_name=(str)(entry.attrib['UserDisplayName'])
                    UserId_or_name = UserId_or_name.replace("'"," ")
                except Exception, e:
                    UserId="not given"
                    count = count+1
            
                insert_str = "insert into `"+ table+ "` values("+entry.attrib['Id']+","+entry.attrib['PostHistoryTypeId']+","+\
                entry.attrib['PostId']+",'"+entry.attrib['RevisionGUID']+"','"+entry.attrib['CreationDate']+"','"+\
                UserId_or_name+"','"+Comment+"','"+Text+"')"
        
                #print "insert str=", insert_str
                insert_cursor.execute(insert_str)
                entry.clear()
    except Exception, e:
        print Exception,e
        db1.commit()
        print "done"
                
        #row=row+1
        #if(row>=1000):
        #    db1.commit()
        
        #if not (entry.tag == ("entry") and entry.get("type") == typ):
        #    continue
        #info = {}
        #for child in entry.getchildren():
        #    text = _lookup_string(child.text, strmap)
            #info[tag] = text
        #    print "text=",text, child.text
            #if child.tag in keys:
            #    tag = _lookup_string(child.tag, strmap)
            #    text = _lookup_string(child.text, strmap)
            #    info[tag] = text
        #lSongs.append(info)=
       
    #return lSongs

fill_stackoverflow_table(dbfile)
db1.commit()
print "done good"

"""
def _lookup_string(string, strmap):
    #Look up @string in the string map,
    #and return the copy in the map.
    #If not found, update the map with the string.
    
    string = string or ""
    try:
        return strmap[string]
    except KeyError:
        strmap[string] = string
        return string
"""

"""
from lxml import etree

def fast_iter(context, func):
    # http://www.ibm.com/developerworks/xml/library/x-hiperfparse/
    # Author: Liza Daly
    for event, elem in context:
        func(elem)
        elem.clear()
        while elem.getprevious() is not None:
            del elem.getparent()[0]
    del context

def process_element(elem):
    print elem.xpath( 'description/text( )' )

context = etree.iterparse( MYFILE, tag='item' )
fast_iter(context,process_element)
"""